
//
// 20120625 - AH - (AH) Created.
//

package org.soekrat.server.io.db;

import javax.persistence.Id;

import org.soekrat.environment.Sensor;

/**
 * <p>
 *   Represents a
 *   {@link Sensor}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class DbSensor 
{
  private @Id Long id;
  
  private long ownerId=-1;
  
  private String clazz=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DbSensor(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param owner {@link #owner()}.
   * @param clazz {@link #clazz()}.
   */
  public DbSensor(long owner, String clazz){ owner(owner).clazz(clazz); }
  
  /**
   * <p>
   *   Gets unique of key of the sensor.
   * </p>
   * @return Key value or {@code -1} if not assigned (not stored in database).
   */
  public long id(){ return id==null ? -1 : id; }
  
  /**
   * <p>
   *   Sets id of owner of {@code this}.
   * </p>
   * @param id Id of owner.
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code id<0}.
   */
  public DbSensor owner(long id)
  {
    if (id<0)
    {
      throw new IllegalArgumentException("id<0");
    }
    
    this.ownerId=id;
    return this;
  }
  
  /**
   * <p>
   *   Gets id of owner of {@code this}.
   * </p>
   * @return Id or {@code -1} if not sat.
   */
  public long ownerId(){ return this.ownerId; }
  
  /**
   * <p>
   *   Sets name of sensor class.
   * </p>
   * @param clazz Class name.
   * @return {@code this}.
   */
  public DbSensor clazz(String clazz)
  {
    if (clazz==null)
    {
      throw new NullPointerException("clazz");
    }
    
    this.clazz=clazz;
    return this;
  }
  
  /**
   * <p>
   *   Gets name on class name for sensor.
   * </p>
   * @return Class name or {@code null} if not sat.
   */
  public String clazz(){ return this.clazz; }
  
  @Override
  public String toString()
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append("id=").append(id).append(", owner=").append(ownerId)
      .append(", class=").append(clazz);
    
    return sb.toString();
  }
  
}
